package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartInfoFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 李旭
 * @date 2021/10/18 9:16
 * @Description:
 */
@Controller
public class CartInfoController {

    @Autowired
    private CartInfoFeignClient cartInfoFeignClient;

    //加入购物车   == 加
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes
    , HttpServletRequest request) {
        String userId = request.getHeader("userId");
        String userTempId = request.getHeader("userTempId");

        System.out.println("进入加入成功页面");
        cartInfoFeignClient.addToCart(skuId, skuNum);
        //SpringMVC   重定向 如果带参数  不同Request      |||    内部转发  同一个Request对象
        redirectAttributes.addAttribute("sId",skuId);
        redirectAttributes.addAttribute("sn",skuNum);
        redirectAttributes.addAttribute("name","我是好人");
        return "redirect:http://cart.gmall.com/toCart.html";
    }

    //加入购物车   ==   查
    @GetMapping("/toCart.html")
    public String toCart(Long sId, Integer sn,String name, Model model) {
        System.out.println("name:" + name);

        System.out.println("回显加入页面");
        CartInfo cartInfo = cartInfoFeignClient.toCart(sId);
        cartInfo.setSkuNum(sn);
        model.addAttribute("cartInfo", cartInfo);
        return "cart/addCart";
    }
    //去购物车结算
    @GetMapping("/cart.html")
    public String cartList(){

        return "cart/index";
    }
}
